package com.since.data_structure.queue;

/**
 * Created by Sincerity on 2020/5/6.
 * 描述：普通队列
 */
public class OrdinaryQueue {
    private int front;
    private int prev;
    private int maxSize;
    private int[] table;

    public OrdinaryQueue(int maxSize) {
        this.maxSize = maxSize;
        table = new int[maxSize];
        front = -1;
        prev = -1;
    }

    public int getSize() {
        if (table == null)
            throw new NullPointerException("队列没有实例化");
        return prev - front;
    }

    public boolean isEmpty() {
        return prev == front;
    }

    public boolean isFull() {
        return prev == maxSize - 1;
    }

    public void add(int data) {
        if (isFull()) {
            System.out.println("队列已满.无法添加数据");
            return;
        }
        table[++prev] = data;
    }

    public int getQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列是空的");
        }
        return table[++front];
    }

    public void show() {
        if (isEmpty()) {
            System.out.println("队列是空的");
            return;
        }
        for (int i = 0; i < table.length; i++) {
            System.out.printf("table[%d]=%d\n", i, table[i]);
        }
    }

    public int getHeader() {
        if (isEmpty()) {
            throw new RuntimeException("队列是空的");
        }
        return table[++front];
    }

}
